package gui.controller;

import framework.AnnuaireCocola;

import gui.ACLGraphics;
import gui.ViewDirectory;
import gui.assiste.CreateClAssisted;
import gui.assiste.PanelOutput;
import gui.search.AdvencedSearch;
import gui.viewLattice.ViewPDF;

import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenuItem;

import aclInfoCollector.view.launcher.DialogInfoGood;

public class Controller implements ActionListener,MouseListener{

	private ACLGraphics acl;
	private AnnuaireCocola cocola;

	public Controller(ACLGraphics acl){
		this.acl = acl;
		this.cocola = acl.getAnnuaireCocola();
	}


	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String action = e.getActionCommand();

		if (action.equals(acl.NEW_CLASSIFICATION)){
			ArrayList<Object> args = CreateClAssisted.openForm();
			acl.addClassification(args);
		}

		if (action.equals(AdvencedSearch.SEARCH)){
			ViewDirectory vv = acl.getCurrentClassification();
			vv.addFrame(vv.getSearch().getCurrentSearch());
		}

		if (action.equals("run")) acl.stopRunning((JMenuItem) e.getSource());

		if (action.equals(acl.OPEN_LIBRARY)) acl.openLibrary();
		if (action.equals(acl.NEW_DIRECTORY)){
			acl.newLibrary(DialogInfoGood.getOutputUrl());
		}
		if (action.equals(acl.GENERATE_COMPONENT)) acl.generate_Components(PanelOutput.getArguments(PanelOutput.IMAGE));
		if (action.equals(acl.GENERATE_INTERFACE)) acl.generate_Interfaces(PanelOutput.getArguments(PanelOutput.IMAGE));
		if (action.equals(acl.GENERATE_FUNCTION)) acl.generate_Fucntions(PanelOutput.getArguments(PanelOutput.IMAGE));
		if (action.equals(acl.GENERATE_COMPONENT_LATEX)) acl.generate_Components_Latex(PanelOutput.getArguments(PanelOutput.PDF));
		if (action.equals(acl.GENERATE_INTERFACE_LATEX)) acl.generate_Interfaces_Latex(PanelOutput.getArguments(PanelOutput.PDF));
		if (action.equals(acl.GENERATE_FUNCTION_LATEX)) acl.generate_Fucntions_Latex(PanelOutput.getArguments(PanelOutput.PDF));
		
		
		
		if (action.equals(ViewDirectory.ALL_REDUCED))acl.getCurrentClassification().positionInternalFrame(action);
		if (action.equals(ViewDirectory.CASCADE))acl.getCurrentClassification().positionInternalFrame(action);
		if (action.equals(ViewDirectory.ALIGN))acl.getCurrentClassification().positionInternalFrame(action);
	}


	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		Object o = e.getSource();

		if (o.getClass() == ViewPDF.class){
			ViewPDF vpdf = (ViewPDF) o;
			String p = vpdf.getPath();
			p = p.substring(0,p.lastIndexOf('.'));
			p = p + ".png";
			acl.getCurrentClassification().addFrameLattice(vpdf.getClassification());
		}
		else if (o.getClass() == JList.class){
			if (e.getClickCount() == 2){
				JList list = (JList) o;
				acl.getCurrentClassification().addFrame((String) list.getSelectedValue());
			}
		}

		acl.update();
	}


	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		Object o = e.getSource();
		if (o.getClass() == ViewPDF.class){
			ViewPDF vpdf = (ViewPDF) o;
			vpdf.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));

		}
	}


	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
	}


	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}


	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

	}


	public ACLGraphics getACLGRaphics(){return acl;}

}
